package com.ruoyi.door.controller.admin.orderinfo;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.door.domain.DoorOrder;
import com.ruoyi.door.domain.DoorOrderDetail;
import com.ruoyi.door.domain.proxy.DoorOrderProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.easy.query.core.api.pagination.EasyPageResult;
import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.easy.query.api.proxy.entity.select.EntityQueryable;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.door.controller.admin.orderinfo.vo.DoorOrderExportVO;
import com.ruoyi.door.utils.DoorOrderExportConverter;

/**
 * 订单信息Controller
 *
 * @author ruoyi
 * @date 2025-06-22
 */
@Api(value ="订单信息管理",tags = "订单信息管理")
@RestController
@RequestMapping("/door/info")
public class DoorOrderInfoController extends BaseController
{
    @Autowired
    private EasyEntityQuery easyEntityQuery;

    private  EntityQueryable<DoorOrderProxy, DoorOrder> buildQuery(DoorOrder doorOrder){
        EntityQueryable<DoorOrderProxy, DoorOrder> queryable = easyEntityQuery.queryable(DoorOrder.class)
        .where(x -> {
                    if(doorOrder.getId() != null){
                        x.id().eq(doorOrder.getId());
                    }

                    if(doorOrder.getType() != null){
                        x.type().eq(doorOrder.getType());
                    }

                    if(doorOrder.getTotalPrice() != null){
                        x.totalPrice().eq(doorOrder.getTotalPrice());
                    }
                    if(doorOrder.getOrderStatus() != null){
                        x.orderStatus().eq(doorOrder.getOrderStatus());
                    }
                    if(doorOrder.getPayTime() != null){
                        x.payTime().eq(doorOrder.getPayTime());
                    }
                    if(StringUtils.isNotBlank(doorOrder.getTradeNo())){
                        x.tradeNo().eq(doorOrder.getTradeNo());
                    }
                    if(StringUtils.isNotBlank(doorOrder.getOrderNo())){
                        x.orderNo().eq(doorOrder.getOrderNo());
                    }
                    if(StringUtils.isNotBlank(doorOrder.getChName())){
                        x.chName().like(doorOrder.getChName());
                    }
                    if(StringUtils.isNotBlank(doorOrder.getChIdNo())){
                        x.chIdNo().like(doorOrder.getChIdNo());
                    }
                    if(StringUtils.isNotBlank(doorOrder.getChMobile())){
                        x.chMobile().like(doorOrder.getChMobile());
                    }

        })
                .orderBy(x -> x.id().desc());
        return queryable;
    }
    /**
     * 查询订单信息列表
     */
    @ApiOperation("查询订单信息列表")
    @GetMapping("/list")
    public TableDataInfo list(DoorOrder doorOrder)
    {
        startPage();
        EasyPageResult<DoorOrder> pageResult = buildQuery(doorOrder)
                .toPageResult(TableSupport.buildPageRequest().getPageNum(),
                        TableSupport.buildPageRequest().getPageSize());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(pageResult.getData());
        rspData.setTotal(pageResult.getTotal());
        return rspData;
    }

    /**
     * 导出订单信息列表
     */
    @ApiOperation("导出订单信息列表")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DoorOrder doorOrder)
    {
        // 查询订单数据
        List<DoorOrder> list = buildQuery(doorOrder).toList();
        
        // 转换为导出VO
        List<DoorOrderExportVO> exportList = DoorOrderExportConverter.convertList(list);
        
        // 导出Excel
        ExcelUtil<DoorOrderExportVO> util = new ExcelUtil<DoorOrderExportVO>(DoorOrderExportVO.class);
        util.exportExcel(response, exportList, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @ApiOperation("获取订单信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        DoorOrder doorOrder = easyEntityQuery.queryable(DoorOrder.class).where(x -> x.id().eq(id))
                .firstOrNull();
        if (ObjectUtil.isNotNull(doorOrder)){
            List<DoorOrderDetail> list = easyEntityQuery.queryable(DoorOrderDetail.class).where(x -> x.orderId().eq(id)).toList();
            doorOrder.setDetails(list);
        }
        return success (doorOrder);
    }

    /**
     * 新增订单信息
     */
    @ApiOperation("新增订单信息")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DoorOrder doorOrder)
    {
        long rows = easyEntityQuery.insertable(doorOrder).executeRows();
        return toAjax(rows>0);
    }

    /**
     * 修改订单信息
     */
    @ApiOperation("修改订单信息")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DoorOrder doorOrder)
    {
        return toAjax(easyEntityQuery.updatable(doorOrder).executeRows()>0);
    }

    /**
     * 删除订单信息
     */
    @ApiOperation("删除订单信息")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        long rows = easyEntityQuery.deletable(DoorOrder.class)
                .disableLogicDelete()//禁用逻辑删除,使用物理删除
            .allowDeleteStatement(true)//如果不允
            .where(x -> x.id().in(ids))
                .executeRows();
        return toAjax(rows>0);
    }
}
